package com.dexilaonian.adpter;


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.dexilaonian.R;
import com.dexilaonian.bean.YiyangtBean;
import com.dexilaonian.utils.Constant;
import com.dexilaonian.utils.ImageLoader;

import java.util.List;
public class YiyangAdapter extends RecyclerView.Adapter<YiyangAdapter.ViewHolder>{
    private final Context context;
    private final List<YiyangtBean> mList_project;
    private MyItemClickListener mItemClickListener;


    public YiyangAdapter(Context context, List<YiyangtBean> list_project) {
        this.context = context;
        mList_project = list_project;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.huli_item, null);
        //将全局的监听传递给holder
        ViewHolder holder = new ViewHolder(view, mItemClickListener);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        String speciality = mList_project.get(position).getSpeciality();
        int times = mList_project.get(position).getTimes();
        String Level_name = mList_project.get(position).getLevel_name();
        String sex = mList_project.get(position).getSex();
        String name = mList_project.get(position).getName();
        int myId = mList_project.get(position).getId();
        String pic_url = mList_project.get(position).getPic_url();
        String good = String.valueOf(mList_project.get(position).getGood());
        if (!TextUtils.isEmpty(name)) {
            holder.tv_titleYiyang.setText(name);
        }
        if (!TextUtils.isEmpty(Level_name)) {
            holder.huliShi.setText(Level_name);
        }
        if (!TextUtils.isEmpty(good)) {
            holder.minYiyang.setText(good);
        }
        if (!TextUtils.isEmpty(pic_url)) {
            String pro_url = Constant.base_pic_url + pic_url;
            ImageLoader.display2(context, pro_url, holder.RecyclerYiyang);
        }
        if (sex.equals("女")) {
            holder.sexYiyang.setImageResource(R.drawable.nv);
        } else {
            holder.sexYiyang.setImageResource(R.drawable.nan);
        }
        holder.yiyangTime.setText(times + "次");

    }

    @Override
    public int getItemCount() {
        if (mList_project.size() == 0) {
            return 0;
        } else {
            return mList_project.size();
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        MyItemClickListener mListener;
        TextView tv_titleYiyang;
        TextView huliShi;
        TextView minYiyang;
        TextView yiyangTime;
        ImageView sexYiyang;
        ImageView RecyclerYiyang;

        public ViewHolder(View itemView, MyItemClickListener myItemClickListener) {
            super(itemView);
            this.mListener = myItemClickListener;
            itemView.setOnClickListener(this);
            tv_titleYiyang = (TextView) itemView.findViewById(R.id.tv_title_yiyang);
            minYiyang = (TextView) itemView.findViewById(R.id.tv_min_yiyang);
            huliShi = (TextView) itemView.findViewById(R.id.hulishi);
            yiyangTime = (TextView) itemView.findViewById(R.id.tv_yiyang_time);
            sexYiyang = (ImageView) itemView.findViewById(R.id.iv_sex_yiyang);
            RecyclerYiyang = (ImageView) itemView.findViewById(R.id.iv_recycler_yiyang);

        }

        @Override
        public void onClick(View v) {
            if (mListener != null) {
                mListener.onItemClick(v, getPosition());
            }

        }
    }
    /**
     * 创建一个回调接口
     */
    public interface MyItemClickListener {
        void onItemClick(View view, int position);
    }

    /**
     * 在activity里面adapter就是调用的这个方法,将点击事件监听传递过来,并赋值给全局的监听
     *
     * @param myItemClickListener
     */
    public void setItemClickListener(MyItemClickListener myItemClickListener) {
        this.mItemClickListener = myItemClickListener;
    }
}
